<template>
  <div>
     <Row>
        <form enctype="multipart/form-data">
              <input type="file" icon="ios-download-outline" @change="onFileChange">
              <Button type="ghost" icon="ios-cloud-upload-outline" @click="onImport(this)" >导入全表Excel</Button>
        </form>
        
        <input v-model="taskId" placeholder="taskId" ></input>
        <Button type="primary" @click="onSearch">查询</Button>
        <Button type="primary" size="large" @click="onExport"  icon="ios-download-outline"></Icon>导出全表EXCEL</Button>
    </Row>
    <Table ref="table" class="bottom-table" :border="true" :stripe="true" :show-header="true" :data="tableData" :columns="tableColumns" ></Table>
    <Page class="bottom-page" :total="totalPage" :current="crtPage" :page-size="pageSize" show-total show-elevator @on-change="onChange"></Page>
    <Modal
        title="编辑Task"
        v-model="editTaskModal" ok-text="提交" @on-ok="onSubmit">
      <Form  label-position="left" :model="formEdit" :label-width="100">
        <Form-item label="taskId">
            <Input v-model="formEdit.taskId" disabled ></Input>
        </Form-item>
        <Form-item label="taskLevel">
            <Input v-model="formEdit.taskLevel" ></Input>
        </Form-item>
        <Form-item label="taskType">
            <Input v-model="formEdit.taskType" ></Input>
        </Form-item>
        <Form-item label="gameId">
            <Input v-model="formEdit.gameId"></Input>
        </Form-item>
        <Form-item label="symbolsStr">
            <Input v-model="formEdit.symbolsStr" ></Input>
        </Form-item>
        <Form-item label="symbolsType">
            <Input v-model="formEdit.symbolsType" ></Input>
        </Form-item>
        <Form-item label="num">
            <Input v-model="formEdit.num" ></Input>
        </Form-item>
        <Form-item label="prs">
            <Input v-model="formEdit.prs" ></Input>
        </Form-item>
        <Form-item label="rewardPoint">
            <Input v-model="formEdit.rewardPoint" ></Input>
        </Form-item>
        <Form-item label="rewardJade">
            <Input v-model="formEdit.rewardJade" ></Input>
        </Form-item>
      </Form>
    </Modal>
  </div>
</template>
<script>

import {getTasks, getAllTasks, modifyTask, importTasks} from '@/api/game.js'
import {getVO} from '@/common/proto-vo.js'
import {exportExcel, importExcel} from '@/common/excelUtil.js'
export default {
  data () {
    return {
      tableData: [],
      editTaskModal: false,
      results: '',
      taskId: '',
      crtPage: 1,
      totalPage: 0,
      pageSize: 10,
      filename: 'task',
      formEdit: {
        taskId: '',
        taskLevel: '',
        taskType: '',
        gameId: '',
        symbolsStr: '',
        symbolsType: '',
        num: '',
        prs: '',
        rewardPoint: '',
        rewardJade: ''
      }
    }
  },
  created () {
    this.onSearch()
  },
  methods: {
    onSearch () {
      let tTaskId = -1
      if (this.taskId !== '') {
        tTaskId = parseInt(this.taskId)
      }

      console.log('tTaskId:', tTaskId, ',element:', this.element, ',crtPage:', this.crtPage, ',pageSize:', this.pageSize)
      getTasks(this.crtPage, this.pageSize, tTaskId).then((data) => {
        if (!data) {
          return
        }

        this.tableData = []
        let TaskVO = getVO('TaskVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = TaskVO.decode(pv.data[i])
          vo.taskId = vo.taskId.toNumber()
          vo.gameId = vo.gameId.toNumber()
          vo.rewardPoint = vo.rewardPoint.toNumber()
          vo.rewardJade = vo.rewardJade.toNumber()
          this.tableData.push(vo)
        }
        this.totalPage = pv.intValues[1]
        console.log(this.tableData)
      })
    },
    onChange (item) {
      console.log(item)
      this.crtPage = item
      this.onSearch()
    },
    onUploadSuccess (response, file, fileList) {
      this.$Message.info('load success!!!')
      console.log(response)
      console.log(file)
      console.log(fileList)
    },
    showEditTask (index) {
      let data = this.tableData[index]
      this.formEdit.taskId = data.taskId
      this.formEdit.taskLevel = data.taskLevel
      this.formEdit.taskType = data.taskType
      this.formEdit.gameId = data.gameId
      this.formEdit.symbolsStr = data.symbolsStr
      this.formEdit.symbolsType = data.symbolsType
      this.formEdit.num = data.num
      this.formEdit.prs = data.prs
      this.formEdit.rewardPoint = data.rewardPoint
      this.formEdit.rewardJade = data.rewardJade

      this.editTaskModal = true
    },
    onSubmit () {
      if (this.formEdit.taskId === '' || this.formEdit.taskLevel === '' || this.formEdit.taskType === '' || this.formEdit.gameId === '' || this.formEdit.symbolsStr === '' || this.formEdit.symbolsType === '' || this.formEdit.num === '' || this.formEdit.prs === '' || this.formEdit.rewardPoint === '' || this.formEdit.rewardJade === '') {
        this.$Message.error('编辑参数不能为空!!!')
        return
      }

      let TaskVO = getVO('TaskVO')
      var taskVO = new TaskVO()
      taskVO.taskId = Number(this.formEdit.taskId)
      taskVO.taskLevel = parseInt(this.formEdit.taskLevel)
      taskVO.taskType = parseInt(this.formEdit.taskType)
      taskVO.gameId = Number(this.formEdit.gameId)
      taskVO.symbolsStr = this.formEdit.symbolsStr
      taskVO.symbolsType = parseInt(this.formEdit.symbolsType)
      taskVO.num = parseInt(this.formEdit.num)
      taskVO.prs = parseInt(this.formEdit.prs)
      taskVO.rewardPoint = Number(this.formEdit.rewardPoint)
      taskVO.rewardJade = Number(this.formEdit.rewardJade)

      modifyTask(taskVO).then((data) => {
        if (!data) {
          return
        }
        let success = data.data.data.intValues[0]
        if (success === 1) {
          let len = this.tableData.length
          for (let i = 0; i < len; i++) {
            let data = this.tableData[i]
            if (data.taskId === this.formEdit.taskId) {
              this.tableData[i].taskLevel = this.formEdit.taskLevel
              this.tableData[i].taskType = this.formEdit.taskType
              this.tableData[i].gameId = this.formEdit.gameId
              this.tableData[i].symbolsStr = this.formEdit.symbolsStr
              this.tableData[i].symbolsType = this.formEdit.symbolsType
              this.tableData[i].num = this.formEdit.num
              this.tableData[i].prs = this.formEdit.prs
              this.tableData[i].rewardPoint = this.formEdit.rewardPoint
              this.tableData[i].rewardJade = this.formEdit.rewardJade
              break
            }
          }
        }
      })
    },
    onImport (obj) {
      // 导入EXCEL
      importTasks(this.results).then((data) => {
        if (!data) {
          return
        }
        let success = data.data.data.intValues[0]
        if (success === 1) {
          this.$Message.info('导入excel成功!!!')
          this.onSearch()
        }
      })
    },
    onExport () {
      // 导出EXCEL
      getAllTasks().then((data) => {
        if (!data) {
          return
        }

        let allTableData = []
        let TaskVO = getVO('TaskVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = TaskVO.decode(pv.data[i])
          vo.taskId = vo.taskId.toNumber()
          vo.gameId = vo.gameId.toNumber()
          vo.rewardPoint = vo.rewardPoint.toNumber()
          vo.rewardJade = vo.rewardJade.toNumber()
          allTableData.push(vo)
        }
        console.log(allTableData)
        exportExcel(this.filename, allTableData)
      })
    },
    onFileChange (e) {
      var files = e.target.files || e.dataTransfer.files
      if (!files.length) {
        return
      }
      importExcel(e.target, this.setResults)
    },
    setResults (res) {
      this.results = res
      console.log('this.results:', this.results)
    }
  },
  computed: {
    tableColumns () {
      let cols = [
        {
          title: 'taskId',
          key: 'taskId',
          align: 'center',
          fixed: 'left'
        },
        {
          title: 'taskLevel',
          key: 'taskLevel',
          align: 'center'
        },
        {
          title: 'taskType',
          key: 'taskType',
          align: 'center'
        },
        {
          title: 'gameId',
          key: 'gameId',
          align: 'center'
        },
        {
          title: 'symbolsStr',
          key: 'symbolsStr',
          align: 'center'
        },
        {
          title: 'symbolsType',
          key: 'symbolsType',
          align: 'center'
        },
        {
          title: 'num',
          key: 'num',
          align: 'center'
        },
        {
          title: 'prs',
          key: 'prs',
          align: 'center'
        },
        {
          title: 'rewardPoint',
          key: 'rewardPoint',
          align: 'center'
        },
        {
          title: 'rewardJade',
          key: 'rewardJade',
          align: 'center'
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          fixed: 'right',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                on: {
                  click: () => {
                    let idx = params.index
                    this.showEditTask(idx)
                  }
                }
              }, '编辑')
            ])
          }
        }
      ]
      return cols
    }
  }
}

</script>

<style scoped>
.log-str{
  font-size: 14px;
  margin-bottom: 2px;
}
.content{
  min-height: 460px;
}
.bottom-page{
  margin-top: 5px;
  margin-bottom: 10px;
  float: right;
}

.bottom-table{
  margin-top: 5px;
  margin-bottom: 10px;
}
</style>
